/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spring;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import phys.CollisionResolver;
import phys.State;
import vec.Vector3f;

/**
 *
 * @author Vic
 */
public class Ball extends Element {

    Vector3f start;
    int RADIUS=10;
    ArrayList<Element> forces;
    Vector3f gravity = new Vector3f(0, -40000, 0);
    Rectangle bounds;

    public Ball(Vector3f start, Rectangle bounds) {
        this.state = new State();
        this.state.setMass(1);
        this.start = start;
        this.forces = new ArrayList<>();
        this.bounds = bounds;
        CollisionResolver.add(this);
    }

    @Override
    public void draw(Graphics g, Vector3f origen) {
        Graphics2D g2d = (Graphics2D) g;
        g.fillOval((int) getPosition().x - RADIUS, (int) (2 * start.y - getPosition().y - RADIUS), 2* RADIUS, 2*RADIUS);
        Vector3f acc = (Vector3f) getAcceleration(state, 0).scale(.01f);
//         g.drawLine((int) getPosition().x - 10, (int) (2 * start.y - getPosition().y - 10),
//                 (int) (getPosition().x - 10+acc.x), (int) (2 * start.y - getPosition().y - 10+acc.y));
        Point2D.Float startPoint = new Point2D.Float(getPosition().x, (2 * start.y - getPosition().y));
        Point2D.Float endPoint = new Point2D.Float((getPosition().x + (getVelocity().x) / 5),
                (2 * start.y - getPosition().y - (getVelocity().y) / 5));
        g2d.draw(new Line2D.Float(startPoint, endPoint));
        drawArrow(g2d, Math.atan2(startPoint.y - endPoint.y, startPoint.x - endPoint.x), endPoint.x, endPoint.y);
        endPoint = new Point2D.Float((getPosition().x + (acc.x)),
                (2 * start.y - getPosition().y - (acc.y)));
        g2d.setColor(Color.red);
        g2d.draw(new Line2D.Float(startPoint, endPoint));
        drawArrow(g2d, Math.atan2(startPoint.y - endPoint.y, startPoint.x - endPoint.x), endPoint.x, endPoint.y);
        g2d.setColor(Color.black);
    }

    void drawArrow(Graphics2D g2, double theta, double x0, double y0) {
        double phi = 10;
        double barb = 10;
        double x = x0 - barb * Math.cos(theta + phi);
        double y = y0 - barb * Math.sin(theta + phi);
        g2.draw(new Line2D.Double(x0, y0, x, y));
        x = x0 - barb * Math.cos(theta - phi);
        y = y0 - barb * Math.sin(theta - phi);
        g2.draw(new Line2D.Double(x0, y0, x, y));
    }

    public void addForce(Element acc) {
        forces.add(acc);
    }

    public void removeForce(Element acc) {
        forces.remove(acc);
    }

    @Override
    public Vector3f getAcceleration(State state, float t) {
        Vector3f force = new Vector3f();
        for (Element acc : forces) {
            force.add(acc.getAppliedForce(this, t));
        }
        //force.add(gravity);


        return force;
    }


    @Override
    public boolean collides(Vector3f point) {
        return StrictMath.hypot(point.x - getPosition().x, point.y - getPosition().y) < RADIUS;
    }

    @Override
    public Rectangle getBounds() {
        return new Rectangle((int)(getPosition().x-RADIUS), (int)(getPosition().y-RADIUS), 2*RADIUS, 2*RADIUS);
    }

    @Override
    public Vector3f getMaxInDir(Vector3f dir) {
        dir.normalize();
        dir.scale(RADIUS);
        return new Vector3f(dir);
    }

    @Override
    public boolean isPassive() {
        return false;
    }

    @Override
    public Vector3f getAppliedForce(Element acc, float t) {
        return new Vector3f();
    }
}
